home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Text⁄Files / macgzip_02-src / macos / think / SPDCProg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-10  |  7.1 KB  |  286 lines  |  [TEXT/KAHL]

  1. /*
  2.  * Copyright (C) 1993  SPDsoft
  3.  * 
  4.  */
  5.  
  6. #include "SPDProg.h"
  7. /*            SpinCursors by
  8.  *            America Online: LISPer
  9.  *             Internet: tree@uvm.edu
  10. */
  11. #define HiWrd(aLong)    (((aLong) >> 16) & 0xFFFF)
  12. #define MAX_L_STR    54
  13.  
  14. typedef struct                /* The structure of an 'acur' resource */
  15. {
  16.     short numberOfFrames;        /* number of cursors to animate */
  17.     short whichFrame;        /* current frame number */
  18.     CursHandle frame[];        /* Pointer to the first cursor */
  19. } acur, *acurPtr, **acurHandle;
  20.  
  21. unsigned long int    SPDBackCycles;
  22.  
  23. Boolean        UseAnimatedCursor=false;
  24. Boolean        CanBreak=false;
  25. acurHandle    gFrameList;
  26.  
  27. Boolean InitAnimatedCursors(short acurID)
  28. {
  29.     register short i=0;
  30.     register short cursID;
  31.     Boolean noErrFlag = FALSE;
  32.     SPDBackCycles=0;
  33.     
  34.     if((gFrameList = (acurHandle) GetResource('acur',acurID))) {
  35.         /* got it! */
  36.         noErrFlag = TRUE;
  37.         while((i<(*gFrameList)->numberOfFrames) && noErrFlag) {
  38.             /* The id of the cursor is stored in the high word of the frame handle */
  39.             cursID = (int) HiWrd((long) (*gFrameList)->frame[i]);
  40.             (*gFrameList)->frame[i] = GetCursor(cursID);
  41.             if((*gFrameList)->frame[i])
  42.                 i++;            /* get the next one */
  43.             else
  44.                 noErrFlag=FALSE;    /* foo! we couldn't find the cursor */
  45.         }
  46.     }
  47.     if(noErrFlag) {
  48.  
  49.         (*gFrameList)->whichFrame = 0;
  50.         UseAnimatedCursor=true;
  51.     }
  52.     return noErrFlag;
  53. }
  54.  
  55. /* Free up the storage used by the current animated cursor and all
  56.    of its frames */
  57. void ReleaseAnimatedCursors()
  58. {
  59.     int i;
  60.     
  61.     if(UseAnimatedCursor)
  62.     {
  63.         for(i=0;i<(*gFrameList)->numberOfFrames;i++)
  64.             ReleaseResource((Handle) (*gFrameList)->frame[i]);
  65.         ReleaseResource((Handle) gFrameList);
  66.     }
  67.     UseAnimatedCursor=false;
  68.     SetCursor(&arrow);
  69.  
  70. }
  71.  
  72. /************************************************************************************/
  73.  
  74. /* Opens the MModal dialog. */
  75. /* Just for grins, I'm also saving and restoring it's position in*/
  76. /* a very simple way */
  77.  
  78. void DrawMovable(WindowPtr drawIt);
  79. void UpdateBar(void);
  80.  
  81. Boolean    UseMModalProg=false;
  82.  
  83. static Rect        barRect; 
  84. static Rect        greyRect;
  85. static short int mypat[] = { 0x55AA,0x55AA,0x55AA,0x55AA };    /* Grey */
  86. unsigned long int SPDEnd, SPDNow;
  87.  
  88. Str255    SPDPstr="\p";
  89. char*    SPDpstr=(char*)SPDPstr;
  90.  
  91. static    Boolean        gModalUp;
  92.     Boolean        gSavedPos;
  93.     Point        gSavedPoint;
  94.  
  95. #define    kMyModalKind    1000
  96.  
  97. void InitMovableModal(unsigned long int theEnd)
  98. {
  99.     WindowPtr temp;
  100.  
  101.     temp = GetNewWindow(150, nil, (WindowPtr)-1);
  102.     /* install drawing proc */
  103.     SetWRefCon(temp, (long)DrawMovable);
  104.     
  105.     /* set the kind to my MModalwindow */
  106.     ((WindowPeek)temp)->windowKind = kMyModalKind;
  107.     gModalUp = true;
  108.     
  109.     if (gSavedPos) {
  110.         /* move it to the saved position */
  111.         /* move it to 0,0 to avoid any math at all */
  112.         MoveWindow(temp, 0, 0, false);
  113.         MoveWindow(temp, gSavedPoint.h, gSavedPoint.v, false);
  114.     }
  115.     
  116.     SPDNow=0;
  117.     SPDEnd=(theEnd==0?1:theEnd);
  118.  
  119.     UseMModalProg=true;
  120.     
  121.         
  122.     CtoPstr(SPDPstr);
  123.     
  124.     if((unsigned char)*SPDPstr > MAX_L_STR)
  125.     {
  126.         *SPDPstr = (unsigned char)MAX_L_STR;
  127.         *(SPDPstr+MAX_L_STR) = '…';
  128.     }
  129.     
  130.     barRect.left=temp->portRect.left+20;
  131.     barRect.right=temp->portRect.right-20;
  132.     barRect.bottom=temp->portRect.bottom - 20;
  133.     barRect.top=barRect.bottom-20;
  134.     
  135.     greyRect.left=barRect.left+1;
  136.     greyRect.right=greyRect.left;
  137.     greyRect.bottom=barRect.bottom-1;
  138.     greyRect.top=barRect.top+1;
  139.     
  140.     ShowWindow(temp);
  141.  
  142.     SetPort(temp);
  143. }
  144.  
  145. void ReleaseMovableModal(void)
  146. {
  147.     WindowPtr temp = FrontWindow();
  148.  
  149.     if(UseMModalProg)
  150.     {
  151.         /* the front window really should be my modal window */
  152.         /* if it isn't, I'm bailing.*/
  153.         gSavedPos = true;
  154.         gSavedPoint.h = temp->portRect.left;
  155.         gSavedPoint.v = temp->portRect.top;
  156.         LocalToGlobal(&gSavedPoint);
  157.         if (((WindowPeek)temp)->windowKind == kMyModalKind)
  158.             CloseWindow(temp);
  159.     
  160.         gModalUp = false;
  161.         UseMModalProg=false;
  162.     }
  163. }
  164.  
  165. static void DrawMovable(WindowPtr drawIt)
  166. {
  167.     WindowPtr tempWP;
  168.     unsigned long int wbar;
  169.     Str255 stemp;
  170.  
  171.     BeginUpdate(drawIt);
  172.     GetPort(&tempWP);
  173.     SetPort(drawIt);
  174.     
  175.         MoveTo(20,20);/*h,v*/
  176.         DrawString(SPDPstr);
  177.  
  178.     wbar = SPDNow*(barRect.right-barRect.left)/SPDEnd+barRect.left; 
  179.     if ( wbar >= barRect.right ) wbar = barRect.right;
  180.     FrameRect(&barRect);
  181.     greyRect.right=wbar-1;
  182.     FillRect(&greyRect,mypat);
  183.  
  184.     DrawControls(drawIt);
  185.     EndUpdate(drawIt);
  186.     SetPort(tempWP);
  187. }
  188.  
  189. static void UpdateBar(void)
  190. {
  191.     WindowPtr tempWP,drawIt;
  192.     unsigned long int wbar;
  193.  
  194.     drawIt = FrontWindow();
  195.     GetPort(&tempWP);
  196.     SetPort(drawIt);
  197.     
  198.  
  199.     wbar = SPDNow*(barRect.right-barRect.left)/SPDEnd+barRect.left; 
  200.     if ( wbar >= barRect.right ) wbar = barRect.right;
  201.     FrameRect(&barRect);
  202.     greyRect.right=wbar-1;
  203.     FillRect(&greyRect,mypat);
  204.  
  205.     DrawControls(drawIt);
  206.     EndUpdate(drawIt);
  207.     SetPort(tempWP);
  208. }
  209.  
  210. /************************************************************************************/
  211.  
  212. Boolean    SPDSystemTask()
  213. {
  214.     typedef long (*MyProcPtr)();
  215.     
  216.     EventRecord    theEvent;
  217.     MyProcPtr    drawProc;
  218.     WindowPtr    twindow;
  219.     static unsigned long int TheLastTime;
  220.     
  221.     if(UseMModalProg)
  222.     {
  223.         SPDNow+=10;
  224.         UpdateBar();
  225.     }
  226.     GetNextEvent(everyEvent, &theEvent);
  227.     {
  228.     if ((theEvent.what == keyDown)&&(CanBreak))
  229.     {
  230.         if (((theEvent.modifiers & cmdKey) != 0)
  231.         && ((theEvent.message & charCodeMask) == '.' ))
  232.         {
  233.             SetCursor(&arrow);
  234.             return(true);
  235.         }
  236.     }else if ((theEvent.what == updateEvt)&&(UseMModalProg))
  237.     {
  238.                 /* Mkae sure it's my window before I jump through the refCon */
  239.                 /* Why, since DA's have they're own layer in 7.0? */
  240.                 /* BECAUSE there are other people in the universe who will */
  241.                 /* add things to your windowList.BalloonWriter, for example, */
  242.                 /* so you still need to be careful */
  243.                 if (((WindowPeek)theEvent.message)->windowKind == kMyModalKind)
  244.                 {
  245.                     /* get the drawing proc from the refCon */
  246.                     drawProc = (MyProcPtr)GetWRefCon((WindowPtr)theEvent.message);
  247.                     /* jump to it */
  248.  
  249.                     drawProc((WindowPtr)theEvent.message);
  250.  
  251.                 }
  252.         }else if (theEvent.what == mouseDown)
  253.         {
  254.             switch (FindWindow(theEvent.where, &twindow))
  255.             {
  256.                 case inSysWindow:
  257.                                 /* pass to the system */
  258.                                 SystemClick(&theEvent, twindow);
  259.                                 break;
  260.                         case inDrag:
  261.                             if (twindow != FrontWindow() && gModalUp)
  262.                             {
  263.                             /* don't do anything, can't drag a back window */
  264.                                SysBeep(1);
  265.                             } else
  266.                             {
  267.                             DragWindow(twindow, theEvent.where, &qd.screenBits.bounds);
  268.                             }
  269.                             break;
  270.                         default:    break;
  271.  
  272.                 
  273.             }
  274.     }
  275.     
  276.     if(( theEvent.when-TheLastTime > kTicksCursor )&&(UseAnimatedCursor))
  277.     {
  278.         SetCursor(*((*gFrameList)->frame[(*gFrameList)->whichFrame++]));
  279.         if((*gFrameList)->whichFrame == (*gFrameList)->numberOfFrames)
  280.             (*gFrameList)->whichFrame = 0;
  281.             
  282.         TheLastTime=theEvent.when;
  283.     }
  284.     }
  285.     return(false);
  286. }